﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLThuChi.Helpers;
using QLThuChi.Repositories;
using QLThuChi.OtherForms;

namespace QLThuChi
{
    public partial class FrmMain : DevComponents.DotNetBar.Office2007Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void menDangNhap_Click(object sender, EventArgs e)
        {
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void menDangXuat_Click(object sender, EventArgs e)
        {
            GlobalVars.g_CurrentUser = null;
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void menDoiMatKhau_Click(object sender, EventArgs e)
        {
            FrmDoiMatKhau frm = new FrmDoiMatKhau();
            frm.ShowDialog();
        }

        private void menQuanLyLoaiNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyLoaiNguoiDung frm = new FrmQuanLyLoaiNguoiDung();
            frm.ShowDialog();
        }

        private void menQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyNguoiDung frm = new FrmQuanLyNguoiDung();
            frm.ShowDialog();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menQuanLyCongTrinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinh frm = new FrmQuanLyCongTrinh();
            frm.ShowDialog();
        }

        private void menQuanLyGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmQuanLyGiaiDoan frm = new FrmQuanLyGiaiDoan();
            frm.ShowDialog();
        }

        private void menQuanLyKhachHang_Click(object sender, EventArgs e)
        {
            FrmQuanLyKhachHang frm = new FrmQuanLyKhachHang();
            frm.ShowDialog();
        }

        private void menQuanLyHopDong_Click(object sender, EventArgs e)
        {
            FrmQuanLyHopDong frm = new FrmQuanLyHopDong();
            frm.ShowDialog();
        }

        private void menQuanLyNhanCong_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhanCong frm = new FrmQuanLyNhanCong();
            frm.ShowDialog();
        }

        private void menQuanLyLoaiNhanCong_Click(object sender, EventArgs e)
        {
            FrmQuanLyLoaiNhanCong frm = new FrmQuanLyLoaiNhanCong();
            frm.ShowDialog();
        }

        private void menQuanLyBoPhan_Click(object sender, EventArgs e)
        {
            FrmQuanLyBoPhan frm = new FrmQuanLyBoPhan();
            frm.ShowDialog();
        }

        private void menQuanLyVLXD_Click(object sender, EventArgs e)
        {
            FrmQuanLyVLXD frm = new FrmQuanLyVLXD();
            frm.ShowDialog();
        }

        private void menQuanLyDonViTinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyDonViTinh frm = new FrmQuanLyDonViTinh();
            frm.ShowDialog();
        }

        private void menLapPhieuThu_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_Thu;
            frm.ShowDialog();
        }

        private void menChiLuongNhanCong_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_ChiLuong;
            frm.ShowDialog();
        }

        private void menChiMuaVLXD_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_ChiVLXD;
            frm.ShowDialog();
        }

        public void GetGiaiDoan_ChiVLXD(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmLapPhieuChiVLXD frm = new FrmLapPhieuChiVLXD(int.Parse(comp[0]));
            frm.ShowDialog();
        }

        private void menHuongDanSuDung_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, "Helps.chm");
        }

        private void menThongTinLienHe_Click(object sender, EventArgs e)
        {
            FrmThongTinLienHe frm = new FrmThongTinLienHe();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyCongTrinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinh frm = new FrmQuanLyCongTrinh();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmQuanLyGiaiDoan frm = new FrmQuanLyGiaiDoan();
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuThu_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_Thu;
            frm.ShowDialog();
        }

        public void GetGiaiDoan_Thu(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmLapPhieuThu frm = new FrmLapPhieuThu(int.Parse(comp[0]));
            frm.ShowDialog();
            
        }

        public void GetGiaiDoan_ChiLuong(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmLapPhieuChiLuongNC frm = new FrmLapPhieuChiLuongNC(int.Parse(comp[0]));
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuChiLuongNhanCong_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_ChiLuong;
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuChiVLXD_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += GetGiaiDoan_ChiVLXD;
            frm.ShowDialog();
        }

        public void GetConnectionInfo(object sender, EventArgs e)
        {
            XMLProvider _provider = new XMLProvider();

            var eventType = (MyConnectionEvent)e;
            GlobalVars.g_strDataSource = eventType.DataSource;
            GlobalVars.g_strDataBaseName = eventType.DataBaseName;

            // Create config file
            if (bool.Parse(eventType.ConnStatus))
            {
                _provider.XMLWriter(GlobalVars.g_strPathConfig, eventType.DataSource, eventType.DataBaseName, eventType.ConnStatus);
            }
            else
            {
                _provider.XMLWriter(GlobalVars.g_strPathConfig, eventType.DataSource, eventType.DataBaseName, eventType.UserName, eventType.Password, eventType.ConnStatus);
            }

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            string ErrorText = "";

            if (!System.IO.File.Exists(GlobalVars.g_strPathConfig))
            {
                FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                frm.Handler += GetConnectionInfo;
                frm.ShowDialog();
            }
            else
            {
                XMLProvider _provider = new XMLProvider();
                // Load config file to get infos that used to connect to DB
                _provider.GetConnectionInfo(ref ErrorText);
                if (ErrorText != "")
                {
                    MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    // Re-create connection infos
                    FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                    frm.Handler += GetConnectionInfo;
                    frm.ShowDialog();
                }
                else
                {
                    // Get connection string successfull, then open connection
                    var lstNguoiDung = NguoiDungRepository.SelectAll();
                    if (lstNguoiDung.Count > 0)
                    {
                        FrmDangNhap frm = new FrmDangNhap();
                        frm.Handler += ValidateLogin;
                        frm.ShowDialog();
                    }
                    else
                    {
                        // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                        menQuanLyLoaiNguoiDung.Enabled = true;
                        menQuanLyNguoiDung.Enabled = true;
                    }
                }
            }
        }

        public void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserName(username);

            if (GlobalVars.g_CurrentUser == null)
            {
                success = false;
                infoText = "Người dùng này chưa có trong hệ thống";
            }
            else
            {
                GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVars.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Bạn đã nhập sai mật khẩu";
                }
                else
                {
                    infoText = "Chào bạn  " + GlobalVars.g_CurrentUser.TenNguoiDung + "  vào hệ thống !";
                }
            }

            if (!success)
            {
                MessageBox.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmDangNhap frm = new FrmDangNhap();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                MessageBoxEx.Show(infoText);
                if (GlobalVars.g_CurrentUser.LoaiNguoiDung.TenLoaiNguoiDung.ToUpper() != "NHÂN VIÊN")
                {
                    // Admin, enabel chuc nang quan tri nguoi dung
                    menQuanLyLoaiNguoiDung.Enabled = true;
                    menQuanLyNguoiDung.Enabled = true;
                }
                else
                {
                    // Nhan vien
                    menQuanLyLoaiNguoiDung.Enabled = false;
                    menQuanLyNguoiDung.Enabled = false;
                }
            }
        }

        private void menThongKePhieuThu_Click(object sender, EventArgs e)
        {
            FrmThongKePhieuThu frm = new FrmThongKePhieuThu();
            frm.ShowDialog();
        }

        private void menThongKeChiLuongNV_Click(object sender, EventArgs e)
        {
            FrmThongKePhieuChiLuong frm = new FrmThongKePhieuChiLuong();
            frm.ShowDialog();
        }

        private void menThongKeChiVLXD_Click(object sender, EventArgs e)
        {
            FrmThongKePhieuChiVLXD frm = new FrmThongKePhieuChiVLXD();
            frm.ShowDialog();
        }

        private void menCongTrinhGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinhGiaiDoan frm = new FrmQuanLyCongTrinhGiaiDoan();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyCongTrinhGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinhGiaiDoan frm = new FrmQuanLyCongTrinhGiaiDoan();
            frm.ShowDialog();
        }

        private void menNhanCongGiaiDoan_Click(object sender, EventArgs e)
        {
            FrmChonCongTrinhGiaiDoan frm = new FrmChonCongTrinhGiaiDoan();
            frm.Handler += QuanLyNhanCongGiaiDoan;
            frm.ShowDialog();
        }

        public void QuanLyNhanCongGiaiDoan(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            FrmQuanLyNhanCongGiaiDoan frm = new FrmQuanLyNhanCongGiaiDoan(int.Parse(comp[0]));
            frm.ShowDialog();
        }

        private void menThongKeDoanhThu_Click(object sender, EventArgs e)
        {
            FrmThongKeDoanhThu frm = new FrmThongKeDoanhThu();
            frm.ShowDialog();
        }
    }
}